<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    div {
      width: 200px;
      height: 50px;
      display: flex;
      margin: 50px;
    }

    div > input {
      width: 100px;
      text-align: center;
      font-size: 20px;
    }

    div > button {
      width: 50px;
      height: 50px;
      font-size: 22px;
    }
  </style>
</head>
<body>

  <div>
    <button class="sub">-</button>
    <input  type="text" value="1">
    <button class="add">+</button>
  </div>

  <div>
    <button class="sub2">-</button>
    <input class="second" type="text" value="1">
    <button class="add2">+</button>
  </div>

    <script>
        function outer(){
            let count = 1;

            const obj = {
                getCount(){return count},
                setCount(val){count = val}
            }

            return obj;
        }


        const box1 = outer();
        const subBtn = document.querySelector('.sub');
        const addBtn = document.querySelector('.add');
        const inp =  document.querySelector('input');
        subBtn.addEventListener('click',function(){
            let num = box1.getCount();  
            if(num<=1){
                box1.setCount(1)
                inp.value = box1.getCount();
            }else{
                box1.setCount(num-1)
                inp.value = box1.getCount();
            }
            
            
        })
        addBtn.addEventListener('click',function(){
            let num = box1.getCount();
            if(num>=20){
                box1.setCount(20);
                inp.value = box1.getCount();
            }
            else{
                box1.setCount(num+1);
                inp.value = box1.getCount();
            }
        })


        const box2 = outer();
        const subBtn2 = document.querySelector('.sub2');
        const addBtn2 = document.querySelector('.add2');
        const inp2 =  document.querySelector('.second');


        subBtn2.addEventListener('click',function(){
            let num = box2.getCount();  
            if(num<=1){
                box2.setCount(1)
                inp2.value = box2.getCount();
            }else{
                box2.setCount(num-1)
                inp2.value = box2.getCount();
            }
            
            
        })
        addBtn2.addEventListener('click',function(){
            let num = box2.getCount();
            if(num>=20){
                box2.setCount(20);
                inp2.value = box2.getCount();
            }
            else{
                box2.setCount(num+1);
                inp2.value = box2.getCount();
            }
        })
    </script>
</body>
</html>